/**
 * 区县 - 新增或编辑界面
 */
Ext.define("PSI.Customer.DistrictEditForm", {
			extend : "Ext.window.Window",

			config : {
				parentForm : null,
				entity : null,
				city : null
			},

			/**
			 * 初始化组件
			 */
			initComponent : function() {
				var me = this;

				var entity = me.getEntity();

				me.adding = entity == null;

				var buttons = [];
				if (!entity) {
					var btn = {
						text : "保存并继续新增",
						formBind : true,
						handler : function() {
							me.onOK(true);
						},
						scope : me
					};

					buttons.push(btn);
				}

				var btn = {
					text : "保存",
					formBind : true,
					iconCls : "PSI-button-ok",
					handler : function() {
						me.onOK(false);
					},
					scope : me
				};
				buttons.push(btn);

				var btn = {
					text : entity == null ? "关闭" : "取消",
					handler : function() {
						me.close();
					},
					scope : me
				};
				buttons.push(btn);

				Ext.apply(me, {
							title : entity == null ? "新增区县" : "编辑区县",
							modal : true,
							resizable : false,
							onEsc : Ext.emptyFn,
							width : 400,
							height : 140,
							layout : "fit",
							listeners : {
								show : {
									fn : me.onWndShow,
									scope : me
								},
								close : {
									fn : me.onWndClose,
									scope : me
								}
							},
							items : [{
								id : "editDistForm",
								xtype : "form",
								layout : {
									type : "table",
									columns : 1
								},
								height : "100%",
								bodyPadding : 5,
								defaultType : 'textfield',
								fieldDefaults : {
									labelWidth : 40,
									labelAlign : "right",
									labelSeparator : "",
									msgTarget : 'side',
									width : 370,
									margin : "5"
								},
								items : [{
											id : "editCityId",
											xtype : "hidden",
											name : "cityId"
										}, {
											xtype : "hidden",
											name : "id",
											value : entity == null
													? null
													: entity.get("id")
										}, {
											id : "editCityCombo",
											xtype : "combo",
											queryMode : "local",
											editable : false,
											valueField : "id",
											fieldLabel : "市",
											allowBlank : false,
											blankText : "没有输入市",
											beforeLabelTextTpl : PSI.Const.REQUIRED,
											store : Ext.create(
													"Ext.data.ArrayStore", {
														fields : ["id", "text"],
														data : []
													})
										}, {
											id : "editDistName",
											fieldLabel : "区县",
											allowBlank : false,
											blankText : "没有输入区县名称",
											beforeLabelTextTpl : PSI.Const.REQUIRED,
											name : "name",
											value : entity == null
													? null
													: entity.get("name"),
											listeners : {
												specialkey : {
													fn : me.onEditNameSpecialKey,
													scope : me
												}
											}
										}],
								buttons : buttons
							}]
						});

				me.callParent(arguments);
			},

			/**
			 * 保存
			 */
			onOK : function(thenAdd) {
				var me = this;

				Ext.getCmp("editCityId").setValue(Ext.getCmp("editCityCombo")
						.getValue());

				var f = Ext.getCmp("editDistForm");
				var el = f.getEl();
				el.mask(PSI.Const.SAVING);
				var sf = {
					url : PSI.Const.BASE_URL + "Home/Customer/editDistrict",
					method : "POST",
					success : function(form, action) {
						me.__lastId = action.result.id;

						el.unmask();

						PSI.MsgBox.tip("数据保存成功");
						me.focus();
						if (thenAdd) {
							me.clearEdit();
						} else {
							me.close();
						}
					},
					failure : function(form, action) {
						el.unmask();
						PSI.MsgBox.showInfo(action.result.msg, function() {
									Ext.getCmp("editDistName").focus();
								});
					}
				};
				f.submit(sf);
			},

			onEditNameSpecialKey : function(field, e) {
				var me = this;

				if (e.getKey() == e.ENTER) {
					var f = Ext.getCmp("editDistForm");
					if (f.getForm().isValid()) {
						me.onOK(me.adding);
					}
				}
			},

			clearEdit : function() {
				Ext.getCmp("editDistName").focus();

				var editors = [Ext.getCmp("editDistName")];
				for (var i = 0; i < editors.length; i++) {
					var edit = editors[i];
					edit.setValue(null);
					edit.clearInvalid();
				}
			},

			onWndClose : function() {
				var me = this;
				if (me.__lastId) {
					me.getParentForm().freshDistrictGrid();
				}
			},

			onWndShow : function() {
				var me = this;

				var edit = Ext.getCmp("editDistName");
				edit.focus();
				edit.setValue(edit.getValue());

				var storeCity = me.getParentForm().getCityGrid().getStore();
				var comboCity = Ext.getCmp("editCityCombo");
				var store = comboCity.getStore();
				for (var i = 0; i < storeCity.getCount(); i++) {
					var item = storeCity.getAt(i);
					store.add({
								id : item.get("id"),
								text : item.get("name")
							});
				}

				var city = me.getCity();
				if (city) {
					comboCity.setValue(city.get("id"));
				}
			}
		});